import { combineReducers } from 'redux';
import logger from 'redux-logger'
import thunk from 'redux-thunk'
import { composeWithDevTools } from 'redux-devtools-extension'
import { createStore, applyMiddleware } from 'redux'


function loadModules() {
  const context = require.context("./modules", true, /\.js|.ts$/);
  const modulesFiles = context.keys();
  const config = modulesFiles.map((key) => ({ key, name: key.match(/^\.\/(.*)\.\w+$/i)[1] }));
  const modules = config.reduce( (cur, { key, name }) => ({ ...cur, [name]: context(key).default }), {});
  return modules;
};
const rootReducer = combineReducers({...loadModules()});
const store = createStore(rootReducer, composeWithDevTools(applyMiddleware(logger, thunk)));

export default store;